package com.scfire.loginFilter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.scfire.deviceInfoUtil.OperatingSystem;
import com.scfire.deviceInfoUtil.UserAgent;

public class LoginFilter implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain filterChain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest )req;
		HttpSession session = request.getSession();  
		
		String href = request.getRequestURL().toString();
		String url = request.getRequestURL().toString();
		
		if(request.getQueryString()!=null){
			href += ("?")+request.getQueryString();
		}
		//记录未登陆时，最后一次访问的url，因为会重定向到/weixin/login_mobile,login_type_check 所以，该url不需要被记录
		if( !url.endsWith(".css") && !url.endsWith(".js") && 
			!url.endsWith(".png") && !url.endsWith(".jpg") && 
			!url.endsWith(".gif") && !url.endsWith(".map") && 
			!url.endsWith(".min") && !url.endsWith(".txt") && 
			!url.endsWith(".ttf") && !url.endsWith(".ico") &&
			!url.contains("login") && !url.contains("login_type_check")){
			session.setAttribute("LOGIN_REDIRECT_URL", href);
		}
		
		/*
		 *由于spring security插件，有些手持设备登陆过程中， 会跳转到/login/auth方法，并没有跳转到登陆
		 *获取到出错条件，强行跳转到登陆方法
		 */
		String userAgent = request.getHeader("User-Agent");
		String type = this.getDeviceInfo(userAgent);
		String uri = request.getRequestURI();
		if(type.equals("mobile") && uri.equals("/scfire/login/auth")){
			RequestDispatcher dispatcher = request.getRequestDispatcher("/weixin/login_type_check");
			dispatcher.forward(request, resp);
			return;
		}
		filterChain.doFilter(req, resp);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		System.out.println("初始化拦截过滤器！");
	}
	
	//获取设备类型，service写在groovy中，不能调用
	String getDeviceInfo(String userAgent) {
		String device="";
		UserAgent useragent = new UserAgent(userAgent);
		//Mac系统和Linux系统当成pc设备处理
		if(
			useragent.getOperatingSystem().equals(OperatingSystem.MAC_OS)||
			useragent.getOperatingSystem().equals(OperatingSystem.MAC_OS_X) ||
			useragent.getOperatingSystem().equals(OperatingSystem.WINDOWS_7)||
			useragent.getOperatingSystem().equals(OperatingSystem.WINDOWS_8)||
			useragent.getOperatingSystem().equals(OperatingSystem.WINDOWS_81)||
			useragent.getOperatingSystem().equals(OperatingSystem.WINDOWS_XP)||
			useragent.getOperatingSystem().equals(OperatingSystem.WINDOWS_98)||
			useragent.getOperatingSystem().equals(OperatingSystem.WINDOWS_VISTA)||
			useragent.getOperatingSystem().equals(OperatingSystem.WINDOWS_10)||
			useragent.getOperatingSystem().equals(OperatingSystem.LINUX)){
			device="pc";
		}else if(useragent.getOperatingSystem().equals(OperatingSystem.ANDROID4)||
			useragent.getOperatingSystem().equals(OperatingSystem.ANDROID4_TABLET)||
			useragent.getOperatingSystem().equals(OperatingSystem.ANDROID4_WEARABLE)||
			useragent.getOperatingSystem().equals(OperatingSystem.ANDROID3_TABLET)||
			useragent.getOperatingSystem().equals(OperatingSystem.ANDROID2)||
			useragent.getOperatingSystem().equals(OperatingSystem.ANDROID2_TABLET)||
			useragent.getOperatingSystem().equals(OperatingSystem.ANDROID1)||
			useragent.getOperatingSystem().equals(OperatingSystem.ANDROID_MOBILE)||
			useragent.getOperatingSystem().equals(OperatingSystem.ANDROID_TABLET) ||
			useragent.getOperatingSystem().equals(OperatingSystem.iOS8_1_IPHONE)||
			useragent.getOperatingSystem().equals(OperatingSystem.iOS8_IPHONE)||
			useragent.getOperatingSystem().equals(OperatingSystem.iOS7_IPHONE)||
			useragent.getOperatingSystem().equals(OperatingSystem.iOS6_IPHONE)||
			useragent.getOperatingSystem().equals(OperatingSystem.iOS5_IPHONE)||
			useragent.getOperatingSystem().equals(OperatingSystem.MAC_OS_X_IPAD)||
			useragent.getOperatingSystem().equals(OperatingSystem.iOS4_IPHONE)||
			useragent.getOperatingSystem().equals(OperatingSystem.iOS8_1_IPAD)||
			useragent.getOperatingSystem().equals(OperatingSystem.iOS8_IPAD)||
			useragent.getOperatingSystem().equals(OperatingSystem.iOS7_IPAD)||
			useragent.getOperatingSystem().equals(OperatingSystem.iOS6_IPAD)||
			useragent.getOperatingSystem().equals(OperatingSystem.MAC_OS_X_IPHONE)||
			useragent.getOperatingSystem().equals(OperatingSystem.MAC_OS_X_IPOD)){
			device="mobile";
		}else{
			device="pc";
		}
		return device;
	}		

}
